package com.hyzx.qbasic.admin.common.config;

import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * Created by 黄江华 on 2018/10/08.
 */

@Configuration
@PropertySource(value = "classpath:aliyun.properties")
public class AliyunConfig {
    @Value("${accesskeyId}")
    private String accesskeyId;

    @Value("${accesskeySecret}")
    private String accesskeySecret;

    @Value("${oss.endPoint}")
    private String ossEndPoint;

    @Value("${oss.bucketName}")
    private String ossBucketName;

    @Value("${oss.bucketDomain}")
    private String ossBucketDomain;

    @Value("${oss.maxConnections}")
    private int ossMaxConnections;

    @Value("${oss.socketTimeout}")
    private int ossSocketTimeout;

    @Value("${oss.connectionTimeout}")
    private int ossConnectionTimeout;

    @Value("${oss.connectionRequestTimeout}")
    private int ossConnectionRequestTimeout;

    @Value("${oss.idleConnectionTime}")
    private int ossIdleConnectionTime;

    @Value("${oss.maxErrorRetry}")
    private int ossMaxErrorRetry;

    @Value("${oss.image.root.dir}")
    private String imageRootDir;

    @Value("${oss.attachment.root.dir}")
    private String attachmentRootDir;

    @Bean
    public ClientConfiguration ossClientConfiguration() {
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setMaxConnections(this.getOssMaxConnections());
        clientConfiguration.setSocketTimeout(this.getOssSocketTimeout());
        clientConfiguration.setConnectionTimeout(this.getOssConnectionTimeout());
        clientConfiguration.setConnectionRequestTimeout(this.getOssConnectionRequestTimeout());
        clientConfiguration.setIdleConnectionTime(this.getOssIdleConnectionTime());
        clientConfiguration.setMaxErrorRetry(this.getOssMaxErrorRetry());

        return clientConfiguration;
    }

    @Bean(destroyMethod = "shutdown")
    public OSSClient ossClient() {
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(this.getAccesskeyId(), this.getAccesskeySecret());
        OSSClient ossClient = new OSSClient(this.getOssEndPoint(), credentialsProvider, this.ossClientConfiguration());
        return ossClient;
    }

    public String getAccesskeyId() {
        return accesskeyId;
    }

    public void setAccesskeyId(String accesskeyId) {
        this.accesskeyId = accesskeyId;
    }

    public String getAccesskeySecret() {
        return accesskeySecret;
    }

    public void setAccesskeySecret(String accesskeySecret) {
        this.accesskeySecret = accesskeySecret;
    }

    public String getOssEndPoint() {
        return ossEndPoint;
    }

    public void setOssEndPoint(String ossEndPoint) {
        this.ossEndPoint = ossEndPoint;
    }

    public String getOssBucketName() {
        return ossBucketName;
    }

    public void setOssBucketName(String ossBucketName) {
        this.ossBucketName = ossBucketName;
    }

    public String getOssBucketDomain() {
        return ossBucketDomain;
    }

    public void setOssBucketDomain(String ossBucketDomain) {
        this.ossBucketDomain = ossBucketDomain;
    }

    public int getOssMaxConnections() {
        return ossMaxConnections;
    }

    public void setOssMaxConnections(int ossMaxConnections) {
        this.ossMaxConnections = ossMaxConnections;
    }

    public int getOssSocketTimeout() {
        return ossSocketTimeout;
    }

    public void setOssSocketTimeout(int ossSocketTimeout) {
        this.ossSocketTimeout = ossSocketTimeout;
    }

    public int getOssConnectionTimeout() {
        return ossConnectionTimeout;
    }

    public void setOssConnectionTimeout(int ossConnectionTimeout) {
        this.ossConnectionTimeout = ossConnectionTimeout;
    }

    public int getOssConnectionRequestTimeout() {
        return ossConnectionRequestTimeout;
    }

    public void setOssConnectionRequestTimeout(int ossConnectionRequestTimeout) {
        this.ossConnectionRequestTimeout = ossConnectionRequestTimeout;
    }

    public int getOssIdleConnectionTime() {
        return ossIdleConnectionTime;
    }

    public void setOssIdleConnectionTime(int ossIdleConnectionTime) {
        this.ossIdleConnectionTime = ossIdleConnectionTime;
    }

    public int getOssMaxErrorRetry() {
        return ossMaxErrorRetry;
    }

    public void setOssMaxErrorRetry(int ossMaxErrorRetry) {
        this.ossMaxErrorRetry = ossMaxErrorRetry;
    }

    public String getImageRootDir() {
        return imageRootDir;
    }

    public void setImageRootDir(String imageRootDir) {
        this.imageRootDir = imageRootDir;
    }

    public String getAttachmentRootDir() {
        return attachmentRootDir;
    }

    public void setAttachmentRootDir(String attachmentRootDir) {
        this.attachmentRootDir = attachmentRootDir;
    }
}
